<?php

namespace App\Http\Controllers\Api\V1\Site;

use Illuminate\Http\Request;
use Biz\Constants\Pay\PayConstants;
use Biz\Requests\Site\SitePayRequest;
use Biz\Services\Site\SitePayService;
use Biz\Resources\Site\SitePayResource;
use Lumen\Http\Resources\Json\DataResourceResponse;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * PayController.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class PayController extends BaseController
{
    /**
     * @var mixed
     */
    protected $service;

    /**
     * PayController constructor.
     *
     * @param SitePayService $service
     */
    public function __construct(SitePayService $service)
    {
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $site_id = $request->get('site_id');
        $pay     = $this->service->findBySiteId($site_id);

        if (is_null($pay)) {
            $data = [
                'item' => null,
            ];

            return new DataResourceResponse($data);
        }

        switch ($pay->gateway) {
            case PayConstants::GATEWAY_PAY_EASE:
                $pay->load('pay_ease');
                break;
        }

        return new SitePayResource($pay);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param SitePayRequest $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(SitePayRequest $request)
    {
        $pay = $this->service->save($request->all());

        return new SitePayResource($pay);
    }

    /**
     * Display the specified resource.
     *
     * @param string $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $pay = $this->service->show($id);

        return new SitePayResource($pay);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param SitePayRequest $request
     * @param string         $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(SitePayRequest $request, $id)
    {
        $pay = $this->service->edit($id, $request->all());

        return new SitePayResource($pay);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param string $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = $this->service->remove($id);

        return new DataResourceResponse($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param string $id
     * @param string $locale
     *
     * @return \Illuminate\Http\Response
     */
    public function deleteTranslations($id, $locale)
    {
        $result =  $this->service->deleteTranslations($id, $locale);

        return new DataResourceResponse($result);
    }
}
